<template>
  <view>
    <!-- 头部及背景色 -->
    <backdrop />
    <navigation back backColor :title="title" />

    <view class="container">
      <view class="page-container">
        <view class="title flex flex-middle flex-between">
          地址信息
          <view class="right flex flex-middle" @click="settingDefault">
            <u-icon
              name="checkmark-circle-fill"
              size="16"
              color="#31BC4E"
              v-if="formData.isDefault"
            />
            <view class="default-box" v-else></view>
            <text>设为默认</text>
          </view>
        </view>

        <view class="form-item border flex flex-middle flex-between">
          <view class="form-title flex flex-middle">
            姓名
            <text>*</text>
          </view>
          <view class="form-content">
            <input placeholder="请输入您的姓名" v-model="formData.realName" />
          </view>
        </view>

        <view class="form-item border flex flex-middle flex-between">
          <view class="form-title flex flex-middle">
            性别
            <text>*</text>
          </view>
          <view class="form-content">
            <u-radio-group
              class="radio-group"
              placement="row"
              v-model="formData.sex"
            >
              <u-radio label="男" :name="1" />
              <u-radio class="right" label="女" :name="2" />
            </u-radio-group>
          </view>
        </view>

        <view class="form-item border flex flex-middle flex-between">
          <view class="form-title flex flex-middle">
            手机号
            <text>*</text>
          </view>
          <view class="form-content">
            <input
              placeholder="请输入您的手机号"
              type="number"
              maxlength="11"
              v-model="formData.phone"
            />
          </view>
        </view>

        <view class="border">
          <view
            class="form-item flex flex-middle flex-between"
            @click="chooseLocation"
          >
            <view class="form-title flex flex-middle">
              选择地址
              <text>*</text>
            </view>
            <u-icon name="arrow-right" color="#232323" size="14" />
          </view>
          <view class="info">
            <text class="color1" v-if="!formData.streetCommunity">
              请选择地址
            </text>
            <text v-else>{{ formData.streetCommunity }}</text>
          </view>
        </view>

        <view class="border">
          <view class="form-item flex flex-middle flex-between">
            <view class="form-title flex flex-middle">
              详细地址
              <text>*</text>
            </view>
          </view>
          <view class="info">
            <u--textarea
              class="textarea"
              count
              autoHeight
              placeholder="请输入详细地址，例1层101室"
              v-model="formData.detail"
            />
          </view>
        </view>

        <view class="tags" v-if="false">
          <view class="title">地址标签</view>
          <view class="content flex flex-wrap">
            <view
              class="item"
              v-for="(item, index) in [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]"
              :key="index"
              @click="changeTag(index)"
              :class="{ active: tagIndex == index }"
            >
              公司
            </view>
          </view>
        </view>
      </view>
    </view>

    <view class="bottom safe-bottom">
      <view class="bottom-container">
        <view class="page-container safe-bottom">
          <view class="btn flex flex-middle flex-center" @click="submit">
            保存地址
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters } from "vuex";
import { saveAddress, getAddressDetail } from "@/api/address";

export default {
  data() {
    return {
      addressIndex: 0,
      edit: 0,
      tagIndex: 0,
      formData: {
        realName: "",
        sex: 0,
        phone: "",
        streetCommunity: "",
        detail: "",
        latitude: "",
        longitude: "",
        isDefault: false,
      },
    };
  },
  computed: {
    ...mapGetters(["latitude", "longitude", "currentAddress"]),
    title() {
      return `${this.edit ? "修改" : "新增"}地址`;
    },
  },
  onLoad({ edit, id }) {
    this.edit = +edit;
    if (this.edit) {
      this.id = id;
      this.getDetail();
      return;
    }
    this.formData.latitude = this.currentAddress
      ? this.currentAddress.latitude
      : this.latitude;
    this.formData.longitude = this.currentAddress
      ? this.currentAddress.longitude
      : this.longitude;
  },
  methods: {
    async getDetail() {
      this.$loading();
      const { data, code } = await getAddressDetail({ id: this.id });
      this.$loading.close();
      if (code == 200) {
        const {
          realName,
          sex,
          phone,
          streetCommunity,
          detail,
          latitude,
          longitude,
          isDefault,
        } = data;
        this.formData = {
          realName,
          sex,
          phone,
          streetCommunity,
          detail,
          latitude,
          longitude,
          isDefault,
        };
      }
    },
    async submit() {
      if (!this.formData.realName) return this.$toast("请输入姓名");
      if (!this.formData.sex) return this.$toast("请选择性别");
      if (!this.formData.phone) return this.$toast("请输入手机号");
      if (!uni.$u.test.mobile(this.formData.phone + ""))
        return this.$toast("请输入正确的手机号");
      if (!this.formData.streetCommunity) return this.$toast("请选择地址");
      if (!this.formData.detail) return this.$toast("请输入详细地址");

      const params = { ...this.formData };
      if (this.edit) params.id = this.id;

      this.$loading();
      const { code, data } = await saveAddress(params);
      this.$loading.close();
      if (code == 200) {
        this.$toast("保存成功");
        this.$store.commit("SET_EDIT_ADDRESS", true);
        this.$store.commit("SET_CURRENT_ADDRESS", data);
        this.$store.dispatch("getAddress");
        setTimeout(() => {
          this.$navigateBack();
        }, 1000);
      }
    },
    chooseLocation() {
      uni.chooseLocation({
        latitude: this.formData.latitude,
        longitude: this.formData.longitude,
        success: ({ name, latitude, longitude }) => {
          this.formData = {
            ...this.formData,
            streetCommunity: name,
            latitude,
            longitude,
          };
        },
      });
    },
    settingDefault() {
      this.formData.isDefault = !this.formData.isDefault;
    },
    changeTag(index) {
      this.tagIndex = index;
    },
  },
};
</script>

<style scoped lang="scss">
.color1 {
  color: #aeaeae;
}

.container {
  position: relative;
  padding: 0 16px;
  color: #232323;
}

.tags {
  overflow: hidden;

  .active {
    border-color: #fe774c !important;
  }

  .item {
    margin-top: 16px;
    margin-left: 16px;
    height: 20px;
    line-height: 20px;
    padding: 0 4px;
    background: #fff3ee;
    border: 1px solid #fff3ee;
    border-radius: 4px;
    color: #fe774c;
  }

  .title {
    padding-top: 14px;
    float: left;
    font-size: 16px;
    font-weight: 400;
  }
}

.info {
  padding-bottom: 10px;

  .textarea {
    font-size: 14px;
    padding-bottom: 24px;
    min-height: 56px;

    ::v-deep textarea {
      min-height: 56px;
    }
  }
}

.border {
  border-bottom: 1px solid #d9d9d9;
}

.form-item {
  height: 48px;

  .form-title {
    font-size: 16px;

    text {
      color: #fe774c;
      margin-left: 2px;
    }
  }

  .form-content {
    height: 100%;

    .radio-group {
      height: 100%;

      .right {
        margin-left: 16px;
      }
    }

    input {
      font-size: 14px;
      text-align: right;
      width: 40vw;
      height: 100%;
    }
  }
}

.title {
  font-size: 16px;
  font-weight: bold;
  height: 44px;

  .right {
    font-size: 14px;
    font-weight: 400;
    height: 100%;

    .default-box {
      width: 14px;
      height: 14px;
      border: 1px solid #999;
      border-radius: 50%;
    }

    text {
      margin-left: 4px;
    }
  }
}

.bottom {
  height: 72px;

  .bottom-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100vw;
    padding: 16px 0;
    background: #fff;
    box-shadow: 0px 0px 8px 0px rgba(218, 218, 218, 0.5);

    .btn {
      margin: 0 54px;
      height: 40px;
      border-radius: 20px;
      background: #31bc4e;
      color: #fff;
    }
  }
}
</style>